package com.tongji.showroom.view
{
	import com.tongji.showroom.events.ListItemMouseEvent;
	import com.tongji.showroom.view.btnSkin.SearchBtnSkin;
	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.controls.Alert;
	
	import org.robotlegs.mvcs.Mediator;
	
	/**
	 * 
	 * @author yanghanbing
	 * 2011/08/
	 */ 
	public class SelectedFurnitureItemRendererMediator extends Mediator
	{
		[Inject]
		public var view:SelectedFurnitureDataListItemRenderer;
		
		[Embed(source="assets/images/loading.swf")]
		private var EmbedLoading:Class;
		private var loadingMC:MovieClip;
		
		private var loader:Loader;
		
		override public function onRegister():void
		{
			view.enabled = false;
			
			loadingMC = new EmbedLoading();
			loadingMC.x = 15;
			loadingMC.y = 5;
			if ( !view.image.contains(loadingMC))	
			{
				view.image.addChild(loadingMC);
				view.image.addEventListener(Event.COMPLETE, loadingCompleteHandler);
			}
			else
			{
				view.image.removeChild(loadingMC);
				view.enabled = true;
			}
				
			view.addEventListener(MouseEvent.MOUSE_DOWN, itemMouseDownHandler);	
		}
		
		private function loadingCompleteHandler(event:Event):void
		{
			hideLoading();
		}
		
		private function hideLoading():void
		{
			if (view.image.contains(loadingMC))
			{
				view.image.removeChild(loadingMC);
				view.enabled = true;
			}
		}
		
		private function itemMouseDownHandler(event:MouseEvent):void
		{
			//eventMap.mapListener(view, MouseEvent.MOUSE_MOVE, itemMouseMoveHandler);
			view.addEventListener(MouseEvent.MOUSE_MOVE, itemMouseMoveHandler);

		}
		
		private function itemMouseMoveHandler(event:MouseEvent):void
		{
			//dispatch(new ListItemMouseEvent(ListItemMouseEvent.ITEM_MOUSE_DOWN, (view.image.content as Bitmap)));
		  view.dispatchEvent(new ListItemMouseEvent(ListItemMouseEvent.ITEM_MOUSE_DOWN, event, (view.image.content as Bitmap).bitmapData.clone()));	
		}
	}
}